#pragma once

#include <unordered_map>
#include <string>
#include <shared_mutex>
#include <condition_variable>
#include <fstream>
#include <bitset>

#include <cassert>

#include "types and marcos.hpp"
#include "my_log.hpp"
#include "module signal.hpp"

struct user
{
    std::string _name;
    std::string _account;
    std::string _password;
};

class user_list
{
public:
    user_list()
    {
        std::fstream _user_database(USER_INFO_PATH);
        if (!_user_database)
        {  
            lg.basic_print(level::FATAL, std::make_tuple("connot open database: "), true, true);
            std::terminate();
        }
        userid_t id;
        user usr;
        while (_user_database >> id >> usr._name >> usr._account >> usr._password)
        {
            if (id == 0)
            {
                continue;
            }
            _id_to_user[id] = usr;
            _id_map[id] = 1;
        }
    }
private:
    std::shared_mutex _id_map_lock;
    std::condition_variable_any _id_map_wait;
    std::unordered_map<userid_t, user> _id_to_user;
    std::bitset<MAX_USER> _id_map;
};